﻿using System.Reflection;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace DAL.Helpers
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        public static ISession OpenSession()
        {
            if (_sessionFactory == null)
            {
                _sessionFactory = CreateSessionFactory();
            }
            
            var session = _sessionFactory.OpenSession();
            return session;
        }

        private static ISessionFactory CreateSessionFactory()
        {
            var sessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                              //.ConnectionString(m => m.Server(@"DANIJEL-PC")
                              //.ConnectionString(m => m.Server(@"127.0.0.1")
                              .ConnectionString(m => m.Server(@"mssql.cloud.hr")
                                                         //.Database("BanDB")
                                                         .Database("zelenjav_BanDB")
                                                         //.TrustedConnection()
                                                         .Username("zelenjav_zelenjava").Password("zelenjava")
                                                         ))
                .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())).BuildSessionFactory();

            return sessionFactory;
        }
    }
}
